package com.baijiayun.liveuibase.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;
import android.util.AttributeSet;

import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.base.BaseUIConstant.ResStateType;
import com.baijiayun.liveuibase.utils.DisplayUtils;
import com.baijiayun.liveuibase.utils.ThemeDataUtil;
import com.baijiayun.liveuibase.utils.drawable.DrawableBuilder;
import com.baijiayun.liveuibase.utils.drawable.StateListDrawableBuilder;

/**
 * Created by pengwenjian on 2020/6/30
 * Describe:
 */
public class StateButton extends AppCompatButton {
    private int stateColor,unStateColor;
    private int stateType,bgType;
    public StateButton(Context context) {
        this(context,null);
    }

    public StateButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public StateButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }
    private void init(Context context,AttributeSet attrs) {
        final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BJYStateButton);
        stateColor = typedArray.getResourceId(R.styleable.BJYStateButton_btnStateColor,0);
        unStateColor = typedArray.getResourceId(R.styleable.BJYStateButton_btnUnStateColor, 0);
        stateType = typedArray.getInt(R.styleable.BJYStateButton_btnStateType, 0);
        bgType = typedArray.getInt(R.styleable.BJYStateButton_btnBgType, 0);
        typedArray.recycle();
        if (stateType == 0) {
            if (stateColor != 0) {
                setTextColor(ThemeDataUtil.getColorFromThemConfigByColorId(context, stateColor));
            }
            if (unStateColor != 0) {
                setTextColor(ThemeDataUtil.getColorFromThemConfigByColorId(context, unStateColor));
            }
        } else {
            setTextColor(DisplayUtils.getColorStateList(ThemeDataUtil.getColorFromThemConfigByColorId(context, stateColor),
                    ThemeDataUtil.getColorFromThemConfigByColorId(context, unStateColor),
                    stateType == 1 ? ResStateType.enable : ResStateType.selected));
        }
       if (bgType == 1) {
            setBackground(new DrawableBuilder()
                    .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_live_product_color))
                    .cornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                    .build());
       } else if (bgType == 2) {
           setBackground(new DrawableBuilder()
                   .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_dialog_negative_bg_color))
                   .cornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                   .build());
       } else if (bgType == 3) {
           //select
           final Drawable selectDrawable = new DrawableBuilder()
                   .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_live_product_color))
                   .cornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                   .build();
           final Drawable drawable = new DrawableBuilder()
                   .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_dialog_negative_bg_color))
                   .cornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                   .build();
           setBackground(new StateListDrawableBuilder()
                   .normal(drawable)
                   .selected(selectDrawable)
                   .build());
       } else if (bgType == 4) {
           //enable
           final Drawable drawable = new DrawableBuilder()
                   .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_live_product_color))
                   .cornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                   .build();
           final Drawable disableDrawable = new DrawableBuilder()
                   .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_dialog_negative_bg_color))
                   .cornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                   .build();
           setBackground(new StateListDrawableBuilder()
                   .normal(drawable)
                   .disabled(disableDrawable)
                   .build());
       }
    }
}
